/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lpsserver;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LOGON
 */
public class RegServer {

    private static RegServer instance;
    private static final int portNumber = 2020;
    private static final String URL = "rmi://localhost:" + Integer.toString(portNumber) + "/";
    private String naming;
    private ArrayList<Remote> listService;

    public void RegisterHost(String naming,Remote remote) throws RemoteException {
        this.naming = naming;
        this.listService.add(remote);
        try {
            LocateRegistry.createRegistry(portNumber);
            String registerURL = RegServer.URL + this.naming;
            Naming.bind(registerURL, this.listService.get(this.listService.size()-1));
        } catch (AlreadyBoundException ex) {
            Logger.getLogger(RegServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(RegServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private RegServer() {
        this.listService = new ArrayList<Remote>();
    }

    public static RegServer getInstance() {
        if (instance == null) {
            instance = new RegServer();
        }
        return instance;
    }
}
